package cn.edu.scau.cmi.pengzhijie.client;

import java.util.List;
import java.util.Scanner;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


import cn.edu.scau.cmi.pengzhijie.hibenate.dao.StudentDao;
import cn.edu.scau.cmi.pengzhijie.hibenate.dao.TeacherDao;
import cn.edu.scau.cmi.pengzhijie.hibenate.domain.Student;
import cn.edu.scau.cmi.pengzhijie.hibenate.domain.Teacher;

public class HibernateClient {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*//第一步： 加载hibernate的核心配置文件
		Configuration configuration = new Configuration();
		configuration.configure();
		//第二步：从配置文件中读取的内容,创建SessionFactory对象
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		//第三步： 用SessionFactory对象创建Session对象
		Session session = sessionFactory.openSession();
		//第四步：开始事务
		Transaction transaction = session.beginTransaction(); 
		//第五步:具体的CRUD操作
		Student student = session.get(Student.class, 2);
		student.setName("zhangsan");
		session.update(student);
		//第六步：提交事务
		transaction.commit();
		//第七步：关闭资源
		session.close();
		sessionFactory.close();*/
		Scanner scanner = new Scanner(System.in);
		StudentDao studentDao = new StudentDao();
		TeacherDao teacherDao = new TeacherDao();
		Student student = new Student();
		Teacher teacher = new Teacher();
		System.out.println("1.添加学生 2.修改学生 3.删除学生 4.添加教师 5.修改教师 6.删除教师 7.学生选择指导老师 8.学生修改指导老师 9.教师选择被指导学生 0.教师修改被指导学生");
		String string = scanner.next();
		int number;
		String str;
		List studentlist = null;
		List teacherlist = null;
		Set<Student> set = null;
		switch (string) {
		case "1":
			System.out.println("请输入学生的学号");
			number = scanner.nextInt();
			student.setNumber(number);
			System.out.println("请输入学生的姓名");
			str = scanner.next();
			student.setName(str);
			studentDao.save(student);
			break;
		case "2":
			studentlist = studentDao.listAll();
			for(int i = 0; i < studentlist.size(); i++) {
				System.out.print(i + "  " + ((Student) studentlist.get(i)).getName() + "  " + "\n");
			}
			System.out.println("请选择要修改的编号:");
			number = scanner.nextInt();
			student = (Student) studentlist.get(number);
			System.out.println("请输入学生的学号");
			number = scanner.nextInt();
			student.setNumber(number);
			System.out.println("请输入学生的姓名");
			str = scanner.next();
			student.setName(str);
			studentDao.updata(student);
			studentlist = studentDao.listAll();
			for(int i = 0; i < studentlist.size(); i++) {
				System.out.print(i + "  " + ((Student) studentlist.get(i)).getName() + "  " + "\n");
			}
			break;
		case "3":
			studentlist = studentDao.listAll();
			for(int i = 0; i < studentlist.size(); i++) {
				System.out.print(i + "  " + ((Student) studentlist.get(i)).getName() + "  " + "\n");
			}
			System.out.println("请选择要删除的编号:");
			number = scanner.nextInt();
			student = (Student) studentlist.get(number);
			studentDao.delete(student);
			studentlist = studentDao.listAll();
			for(int i = 0; i < studentlist.size(); i++) {
				System.out.print(i + "  " + ((Student) studentlist.get(i)).getName() + "  " + "\n");
			}
			break;
		case "4": 
			System.out.println("请输入老师的教工号");
			number = scanner.nextInt();
			teacher.setId(number);
			System.out.println("请输入老师的姓名");
			string = scanner.next();
			teacher.setName(string);
			teacherDao.save(teacher);
			break;
		case "5":
			teacherlist = (List) teacherDao.listAll();
			System.out.println("请选择对应修改老师的编号:");
			for(int i = 0; i < teacherlist.size(); i++) {
				System.out.print(i + "  " + ((Teacher)teacherlist.get(i)).getName() + "  " + "\n");
			}
			number = scanner.nextInt();
			teacher = (Teacher)teacherlist.get(number);
			System.out.println("请输入老师的姓名");
			string = scanner.next();
			teacher.setName(string);
			teacherDao.updata(teacher);
			break;
		case "6":
		teacherlist = (List) teacherDao.listAll();
		System.out.println("请选择对应修改老师的编号:");
		for(int i = 0; i < teacherlist.size(); i++) {
			System.out.print(i + "  " + ((Teacher)teacherlist.get(i)).getName() + "  " + "\n");
		}
		number = scanner.nextInt();
		teacher = (Teacher)teacherlist.get(number);
		teacherDao.delete(teacher);
			break;
		case "7":
			studentlist = studentDao.listAll();
			for(int i = 0; i < studentlist.size(); i++) {
				System.out.print(i + "  " + ((Student) studentlist.get(i)).getName() + "  " + "\n");
			}
			System.out.println("请选择学生的编号:");
			number = scanner.nextInt();
			student = (Student) studentlist.get(number);
			teacherlist = (List) teacherDao.listAll();
			System.out.println("请选择老师的编号:");
			for(int i = 0; i < teacherlist.size(); i++) {
				System.out.print(i + "  " + ((Teacher)teacherlist.get(i)).getName() + "  " + "\n");
			}
			number = scanner.nextInt();
			teacher = (Teacher)teacherlist.get(number);
			student.setTeacher(teacher);
			studentDao.updata(student);
			break;
		case "8":
			studentlist = studentDao.listAll();
			for(int i = 0; i < studentlist.size(); i++) {
				if(((Student) studentlist.get(i)).getTeacher() != null) {
					System.out.print(i + "  " + ((Student) studentlist.get(i)).getName() + "  指导老师: "  + ((Student) studentlist.get(i)).getTeacher().getName() + "\n");
				} else {
					System.out.print(i + "  " + ((Student) studentlist.get(i)).getName() + "  "  + "\n");

				}
			}
			System.out.println("请选择学生的编号:");
			number = scanner.nextInt();
			student = (Student) studentlist.get(number);
			teacherlist = (List) teacherDao.listAll();
			System.out.println("请选择老师的编号:");
			for(int i = 0; i < teacherlist.size(); i++) {
				System.out.print(i + "  " + ((Teacher)teacherlist.get(i)).getName() + "  " + "\n");
			}
			number = scanner.nextInt();
			teacher = (Teacher)teacherlist.get(number);
			student.setTeacher(teacher);
			studentDao.updata(student);
			break;
		case "9":
			teacherlist = (List)teacherDao.listAll();
			for(int i = 0; i < teacherlist.size(); i++) {
				System.out.println(i + "  " + ((Teacher)teacherlist.get(i)).getName());
				set = ((Teacher)teacherlist.get(i)).getStudents();
			}
			System.out.println("请选择老师的编号:");
			number = scanner.nextInt();
			teacher = (Teacher)teacherlist.get(number);
			set = teacher.getStudents();
			studentlist = studentDao.listAll();
			for(int i = 0; i < studentlist.size(); i++) {
				if(((Student) studentlist.get(i)).getTeacher() != null) {
					System.out.print(i + "  " + ((Student) studentlist.get(i)).getName() + "  当前指导老师: "  + ((Student) studentlist.get(i)).getTeacher().getName() + "\n");
				} else {
					System.out.print(i + "  " + ((Student) studentlist.get(i)).getName() + "  "  + "\n");

				}
			}
			System.out.println("请选择学生的编号:");
			number = scanner.nextInt();
			student = (Student) studentlist.get(number);
			student.setTeacher(teacher);
			studentDao.updata(student);
			break;
		case "0":
			teacherlist = (List)teacherDao.listAll();
			for(int i = 0; i < teacherlist.size(); i++) {
				System.out.println(i + "  " + ((Teacher)teacherlist.get(i)).getName());
				set = ((Teacher)teacherlist.get(i)).getStudents();
			}
			System.out.println("请选择老师的编号:");
			number = scanner.nextInt();
			teacher = (Teacher)teacherlist.get(number);
			set = teacher.getStudents();
			studentlist = studentDao.listAll();
			for(int i = 0; i < studentlist.size(); i++) {
				if(((Student) studentlist.get(i)).getTeacher() != null) {
					System.out.print(i + "  " + ((Student) studentlist.get(i)).getName() + "  当前指导老师: "  + ((Student) studentlist.get(i)).getTeacher().getName() + "\n");
				} else {
					System.out.print(i + "  " + ((Student) studentlist.get(i)).getName() + "  "  + "\n");

				}
			}
			System.out.println("请选择学生的编号:");
			number = scanner.nextInt();
			student = (Student) studentlist.get(number);
			student.setTeacher(teacher);
			studentDao.updata(student);
			break;
		default:
			break;
		}
	}

}
